<?php
class ecAction extends ecTemplate {

  protected $viewParams;
  public $params;
  public $option = array();

  public function __construct($params= array())
  {
    global $debug;
    $this->setParams($params);
    $this->setView();
    $this->setSeo();
    $this->setSettings();
    $this->setDefaultLanguage();
    $this->getTranslation();

    //Instance DB master pour edition and DB slave for selection.
    ecConnectionDatabase::init();
  }

  public function getTranslation($nameFile='website.xml')
  {
    global $lang;
    $lang = new ecXMLI18n(dirname(__FILE__).'/../../../apps/'.$this->params['applicationName'].'/lang/'.$nameFile, substr($this->params['defaultLanguage'], 0, 2));
  }

  public function getParams()
  {
    return $this->params;
  }

  public function setParams($params=array())
  {
    if(!$this->params = $params)
      return false;
    return true;
  }

  public function setViewParams($viewParams = array())
  {
    if(!$this->viewParams = $viewParams)
      return false;
    return true;
  }

  public function setDefaultLanguage()
  {
    if(!$this->params['defaultLanguage'] = ecI18n::preferedLanguage($this->getParams()))
      return false;
    return true;
  }

  private function findSettingsFiles($file, $params = array())
  {
    $statutTab0 = 0;
    $statutTab1 = 0;
    $statutTab2 = 0;
    $stop = 0;

    $yaml = new sfYamlParser();

    $files = sfFinder::type('file')
      ->name($file)
      ->in(dirname(__FILE__).'/../../../settings');

    if(sizeof($files) > 0)
    {
      if (($value0 = apc_fetch('cfg_'.md5($file))) === false)
      {
        $value0 = $yaml->parse(file_get_contents(dirname(__FILE__).'/../../../settings/'.$file));

        apc_store('cfg_'.md5($file), serialize($value0), APC_TIMEOUT);
      }
      else{
        $value0 = unserialize($value0);
      }

      $statutTab0 = 1;
    }

    $files = sfFinder::type('file')
      ->name($file)
      ->in(dirname(__FILE__).'/../../../apps/'.$params['applicationName'].'/modules/'.$params['moduleName']);

    if(sizeof($files) > 0)
    {
      if (($value1 = apc_fetch('cfg_'.$params['applicationName'].'_'.$params['moduleName'].'_'.md5($file))) === false)
      {
        $value1 = $yaml->parse(file_get_contents(dirname(__FILE__).'/../../../apps/'.$params['applicationName'].'/modules/'.$params['moduleName'].'/settings/'.$file));
      
        apc_store('cfg_'.$params['applicationName'].'_'.$params['moduleName'].'_'.md5($file), serialize($value1), APC_TIMEOUT);
      }
      else{
        $value1 = unserialize($value1);
      }

      $statutTab1 = 1;
    }

    $files = sfFinder::type('file')
      ->name($file)
      ->in(dirname(__FILE__).'/../../../apps/'.$params['applicationName'].'/settings/');

    if(sizeof($files) > 0)
    {
      if (($value2 = apc_fetch('cfg_'.$params['applicationName'].'_'.md5($file))) === false)
      {
        $value2 = $yaml->parse(file_get_contents(dirname(__FILE__).'/../../../apps/'.$params['applicationName'].'/settings/'.$file));

        apc_store('cfg_'.$params['applicationName'].'_'.md5($file), serialize($value2), APC_TIMEOUT);
      }
      else{
        $value2 = unserialize($value2);
      }

      $statutTab2 = 1;

    }

    

    if($statutTab0 == 1 && $statutTab1 == 1 && $statutTab2 == 1)
    {
      $tab = array_merge($value0, $value1, $value2);
    }
    elseif($statutTab0 == 1 && $statutTab1 == 1 && $statutTab2 == 0)
    {
        $tab = array_merge($value0, $value1);
    }
    elseif($statutTab0 == 1 && $statutTab1 == 0 && $statutTab2 == 1)
    {
        $tab = array_merge($value0, $value2);
    }
    elseif($statutTab0 == 0 && $statutTab1 == 1 && $statutTab2 == 1)
    {
        $tab = array_merge($value1, $value2);
    }
    elseif($statutTab0 == 1 && $statutTab1 == 0 && $statutTab2 == 0)
    {
      $tab = $value0;
    }
    elseif($statutTab0 == 0 && $statutTab1 == 1 && $statutTab2 == 0)
    {
      $tab = $value1;
    }
    elseif($statutTab0 == 0 && $statutTab1 == 0 && $statutTab2 == 1)
    {
      $tab = $value2;
    }
    else{
      return false;
    }

    return $tab;
  }

  public function setSettings()
  {
    $params = $this->getParams();
    if($tab = $this->findSettingsFiles('settings.yml', $params))
    {
      $array = array();
      
      foreach($tab as $key => $value)
        if($key == 'all' OR $key == 'config')
          foreach($value as $key => $value)
            $array[$key] = $value;

      foreach($tab as $key => $value)
        if($key == $params['moduleName'])
          foreach($value as $key => $value)
            if($key == 'all' OR $key == 'config')
              foreach($value as $key => $value)
                $array[$key] = $value;

      foreach($tab as $key => $value)
        if($key == $params['moduleName'])
          foreach($value as $key => $value)
            if($key == $params['controllerName'])
              foreach($value as $key => $value)
                if($key == 'all' OR $key == 'config')
                  foreach($value as $key => $value)
                    $array[$key] = $value;

      foreach($tab as $key => $value)
        if($key == $params['moduleName'])
          foreach($value as $key => $value)
            if($key == $params['controllerName'])
              foreach($value as $key => $value)
                if($key == $params['actionName'])
                  foreach($value as $key => $value)
                    $array[$key] = $value;

      //settings set
      foreach($array as $key => $value)
      {
          $params[$key] = $value;
      }

      $this->setParams($params);

      return true;
    }
    return false;
  }

  //charge les fichiers view params dans l'ordre suivant où le fichier est placé dans l'arborescence.
  public function setView()
  {
    $params = $this->getParams();

    if($tab = $this->findSettingsFiles('views.yml', $params))
    { 
      $arrayCss = array();
      $arrayJs = array();

      foreach($tab as $key => $value)
        if($key == 'all' OR ($key == 'debug' && ecEnvironment::isDebug() == true))
          foreach($value as $key => $value)
            if($key == 'css')
              foreach($value as $value)
                $arrayCss[] = $value;
            elseif($key == 'js')
              foreach($value as $value)
                $arrayJs[] = $value;

      foreach($tab as $key => $value)
        if($key == $params['moduleName'])
          foreach($value as $key => $value)
            if($key == 'all' OR ($key == 'debug' && ecEnvironment::isDebug() == true))
              foreach($value as $key => $value)
                if($key == 'css')
                  foreach($value as $value)
                    $arrayCss[] = $value;
                elseif($key == 'js')
                  foreach($value as $value)
                    $arrayJs[] = $value;

      foreach($tab as $key => $value)
        if($key == $params['moduleName'])
          foreach($value as $key => $value)
            if($key == $params['controllerName'])
              foreach($value as $key => $value)
                if($key == 'all' OR ($key == 'debug' && ecEnvironment::isDebug() == true))
                  foreach($value as $key => $value)
                    if($key == 'css')
                      foreach($value as $value)
                        $arrayCss[] = $value;
                    elseif($key == 'js')
                      foreach($value as $value)
                        $arrayJs[] = $value;

      foreach($tab as $key => $value)
        if($key == $params['moduleName'])
          foreach($value as $key => $value)
            if($key == $params['controllerName'])
              foreach($value as $key => $value)
                if($key == $params['actionName'])
                  foreach($value as $key => $value)
                        if($key == 'css')
                          foreach($value as $value)
                            $arrayCss[] = $value;
                        elseif($key == 'js')
                          foreach($value as $value)
                            $arrayJs[] = $value;

       $this->setViewParams(array(
        'css' => $arrayCss,
        'js'  => $arrayJs,
        ));
      return true;
    }
    return false;
  }


  public function setSeo()
  {
    $params = $this->getParams();

    if($tab = $this->findSettingsFiles('seo.yml', $params))
    {
      foreach($tab as $key => $value)
        if($key == 'all')
          foreach($value as $key => $value)
            if($key == 'link')
              $this->setLinks($value);
            elseif($key == 'metaName')
              $this->setMetaTags($value);
            elseif($key == 'metaHttpEquiv')
              $this->setMetaTagsHttpEquiv($value);
            elseif($key == 'title')
              $this->setTitle($value);

      foreach($tab as $key => $value)
        if($key == $params['moduleName'])
          foreach($value as $key => $value)
            if($key == 'all')
              foreach($value as $key => $value)
                if($key == 'link')
                  $this->setLinks($value);
                elseif($key == 'metaName')
                  $this->setMetaTags($value);
                elseif($key == 'metaHttpEquiv')
                  $this->setMetaTagsHttpEquiv($value);
                elseif($key == 'title')
                  $this->setTitle($value);

      foreach($tab as $key => $value)
        if($key == $params['moduleName'])
          foreach($value as $key => $value)
            if($key == $params['controllerName'])
              foreach($value as $key => $value)
                if($key == 'all')
                  foreach($value as $key => $value)
                    if($key == 'link')
                      $this->setLinks($value);
                    elseif($key == 'metaName')
                      $this->setMetaTags($value);
                    elseif($key == 'metaHttpEquiv')
                      $this->setMetaTagsHttpEquiv($value);
                    elseif($key == 'title')
                      $this->setTitle($value);

      foreach($tab as $key => $value)
        if($key == $params['moduleName'])
          foreach($value as $key => $value)
            if($key == $params['controllerName'])
              foreach($value as $key => $value)
                if($key == $params['actionName'])
                  foreach($value as $key => $value)
                    if($key == 'link')
                      $this->setLinks($value);
                    elseif($key == 'metaName')
                      $this->setMetaTags($value);
                    elseif($key == 'metaHttpEquiv')
                      $this->setMetaTagsHttpEquiv($value);
                    elseif($key == 'title')
                      $this->setTitle($value);

      return true;
    }
    return false;
  }

  public function getViewParams()
  {
    return $this->viewParams;
  }
}